
//  CYJumpsTool.m
//  CreditAddressBook
//
//  Created by Lee on 15/5/6.
//  Copyright (c) 2015年 Lee. All rights reserved.
//

#import "CYJumpsTool.h"
#import "MJExtension.h"
#import <objc/runtime.h>
#import "CYCompanyInfoTableVC.h"
#import "CYPersonalDetailsVController.h"
#import "CYNavigationVController.h"

@implementation CYJumpsTool

+ (void)jumpToMain:(UIView *)view {
    UIStoryboard *main = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *mainController = [main instantiateInitialViewController];
    view.window.rootViewController = mainController;
}

+ (void)jumpToLogin:(UIViewController *)controller {
    UIStoryboard *main = [UIStoryboard storyboardWithName:@"Login" bundle:nil];
    UIViewController *mainController = [main instantiateInitialViewController];
    [controller presentViewController:mainController animated:YES completion:nil];
}

+ (void)jumpToMyWithController:(UIViewController *)controller backStyle:(CYBackStyle)backStyle {
    CYMyRelease *release = [controller.storyboard instantiateViewControllerWithIdentifier:@"CYMyRelease"];
    release.backStyle = backStyle;
    [controller.navigationController pushViewController:release animated:YES];
}

+ (void)jumpToCompanyLogin:(UIViewController *)controller{
    UIStoryboard *main = [UIStoryboard storyboardWithName:@"CompanyLogin" bundle:nil];
    UIViewController *mainController = [main instantiateInitialViewController];
    [controller presentViewController:mainController animated:YES completion:nil];
}

+ (void)jumpToCompanyInfo:(UIViewController *)controller pushType:(CYPushType)pushType {
    CYCompanyInfoTableVC *info = [CYCompanyInfoTableVC le_loadFromStoryBoard:[self mainSB]];
    info.pushType = pushType;
    if (pushType == CYPushTypePresent) {
        CYNavigationVController *navi = [[CYNavigationVController alloc] initWithRootViewController:info];
        [controller presentViewController:navi animated:YES completion:nil];
    } else {
        [controller.navigationController pushViewController:info animated:YES];
    }
}

+ (void)jumpToUserInfo:(UIViewController *)controller {
    CYPersonalDetailsVController *personDetail = [CYPersonalDetailsVController le_loadFromStoryBoard:[self mainSB]];
    personDetail.pushType = CYPushTypePresent;
    CYNavigationVController *navi = [[CYNavigationVController alloc] initWithRootViewController:personDetail];
    [controller presentViewController:navi animated:YES completion:nil];
}

+ (UIStoryboard *)mainSB {
    return [UIStoryboard storyboardWithName:@"Main" bundle:nil];
}
+ (UIStoryboard *)loginSB {
    return [UIStoryboard storyboardWithName:@"Login" bundle:nil];
}

/**
 *  界面名称
 */
+ (NSString *)interfaceNameWithClassName:(NSString *)name {
    NSString *file = [[NSBundle mainBundle] pathForResource:@"interfaceName.plist" ofType:nil];
    NSDictionary *interfaceNameDic = [NSDictionary dictionaryWithContentsOfFile:file];
    NSString *interfaceName = interfaceNameDic[name];
    if (interfaceName && !CYStringEqualTo(interfaceName, @"")) {
        return interfaceName;
    }
    return name;
}

/**
 *  设置layer的边框圆角
 */
+ (void)viewLayerWithView:(UIView *)view cornerRadius:(CGFloat)cornerRadius border:(CGFloat)border {
    view.layer.cornerRadius = cornerRadius;
    view.layer.masksToBounds = YES;
    view.layer.borderWidth = border;
    view.layer.borderColor = CYNaviThemeColor.CGColor;
}

+ (void)exchangeMethodWhthClass:(Class)class oldSEL:(SEL)oldSEL newClass:(Class)newClass newSEL:(SEL)newSEL {
    Method oldMethod = class_getInstanceMethod(class, oldSEL);
    assert(oldMethod);
    Method newMethod = class_getInstanceMethod(newClass, newSEL);
    assert(newMethod);
    method_exchangeImplementations(oldMethod, newMethod);
    
    // 获取到UIWindow中sendEvent对应的method
//    Method sendEvent = class_getInstanceMethod([UIWindow class], @selector(sendEvent:));
//    Method sendEventMySelf = class_getInstanceMethod([self class], @selector(sendEventHooked:));
    
    // 将目标函数的原实现绑定到sendEventOriginalImplemention方法上
//    IMP sendEventImp = method_getImplementation(sendEvent);
//    class_addMethod([UIWindow class], @selector(sendEventOriginal:), sendEventImp, method_getTypeEncoding(sendEvent));
    
    // 然后用我们自己的函数的实现，替换目标函数对应的实现
//    IMP sendEventMySelfImp = method_getImplementation(sendEventMySelf);
//    class_replaceMethod([UIWindow class], @selector(sendEvent:), sendEventMySelfImp, method_getTypeEncoding(sendEvent));
}
@end
